package p100;

import java.util.*;

public class Leetcode_102 {
    /**
     * Definition for a binary tree node.
     */
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> r = new ArrayList<>();
        if (root == null) {
            return r;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()) {
            int c = q.size();
            List<Integer> list = new ArrayList<>();
            while (c > 0) {
                TreeNode node = q.poll();
                if (node != null) {
                    list.add(node.val);
                    if (node.left != null) {
                        q.add(node.left);
                    }
                    if (node.right != null) {
                        q.add(node.right);
                    }
                }
                c--;
            }
            r.add(list);
        }
        return r;
    }

    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("1", 1);
        map.put("2", 2);
        map.put("3", 4);
        map.forEach((k, v) -> {
            if ("1".equals(k)) {
                map.put(k, 10);
            }
        });
        map.forEach((k, v) -> {
            System.out.println(k + ":" + v);
        });

    }
}
